package com.iyao_app.adapter;

import android.content.Context;
import android.graphics.Color;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeController;
import com.facebook.drawee.generic.RoundingParams;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.iyao_app.R;
import com.iyao_app.entry.DemoData;
import com.iyao_app.util.ResourcesUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 作者 :  胡家兵
 * 时间 :  2017/11/11.
 */

public class PictureAdapter extends BaseRecyclerAdapter<DemoData, PictureAdapter.ViewHolder> {

    public PictureAdapter(Context context, List<DemoData> data) {
        super(context, data);
    }

    @Override
    protected ViewHolder getViewHolder(int viewType) {
        return new ViewHolder(mInflater.inflate(R.layout.item_picture, null));
    }

    @Override
    protected void onBindViewHolder_(ViewHolder holder, final int position) {
        RecyclerView.LayoutParams params = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        holder.sdvPhoto.setLayoutParams(params);

        RoundingParams roundingParams = holder.sdvPhoto.getHierarchy().getRoundingParams();
        DemoData demoData = data.get(position);
        if (demoData.isSelect()) {
            roundingParams.setBorderColor(ResourcesUtils.getColor(R.color.app_orange_ff773d));
            holder.sdvPhoto.getHierarchy()
                    .setRoundingParams(roundingParams);
        } else {
            roundingParams.setBorderColor(Color.TRANSPARENT);
            holder.sdvPhoto.getHierarchy()
                    .setRoundingParams(roundingParams);
        }

        holder.sdvPhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = 0; i < data.size(); i++) {
                    if (position == i) {
                        if (selectListener!=null){
                            selectListener.onSelect(position);
                        }
                        data.get(i).setSelect(true);
                    } else {
                        data.get(i).setSelect(false);
                    }
                }
                notifyDataSetChanged();
            }
        });
        try {
            Uri uri = Uri.parse(demoData.getImage());
            ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                    .setResizeOptions(new ResizeOptions(72, 98))
                    .build();
            PipelineDraweeController controller = (PipelineDraweeController) Fresco.newDraweeControllerBuilder()
                    .setOldController(holder.sdvPhoto.getController())
                    .setImageRequest(request)
                    .build();
            holder.sdvPhoto.setController(controller);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.sdv_photo)
        SimpleDraweeView sdvPhoto;


        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    PictureSelectListener selectListener;

    public void setOnPictureSelectListener(PictureSelectListener onPictureSelectListener) {
        selectListener=onPictureSelectListener;
    }

    public interface PictureSelectListener {
        void onSelect(int position);
    }
}
